Installation of R, RStudio and git

Please install the latest versions of , RStudio, and git.

Having an up-to-date environment is the first good practice.


Installation

 Windows

  1. Download at:
    https://cran.r-project.org/

         Click on Download R for Windows
         Click on base
         Click on Download R 4.1.2 for Windows
         Open R-4.1.2-win.exe and follow instructions


  1. Download RStudio Desktop at:
    https://www.rstudio.com/products/rstudio/download/#download

         Download the version for Windows 10 (the .exe and not the .zip)
         Open RStudio-2021.09.0-351.exe and follow instructions
         Check if RStudio has found . Launch RStudio and run:

R.version.string
## [1] "R version 4.1.2 (2021-11-01)"


  1. Download git at:
    https://git-scm.com/downloads

         Download the 64-bit version for Windows
         Open Git-2.33.1-64-bit.exe and follow instructions
         Check if RStudio has found git. Launch RStudio and run:

Sys.which("git")
## C:/Program Files/Git/bin/git.exe


  1. Download Rtools4 at:
    https://cran.r-project.org/bin/windows/Rtools/

         Download the 64-bit version for Windows
         Open rtools40v2-x86_64.exe and follow instructions

The installation of Rtools4 is optional, but this software is required to install packages (from sources) containing compiled code (C/C++/Fortran). Rtools4 contains in particular the utility make.

         Once Rtools4 is installed, you need to tell where to find Rtools4 utilities. Open RStudio et run:

cat("PATH='${RTOOLS40_HOME}\\usr\\bin;${PATH}'\r\n", file = "~/.Renviron", append = TRUE)

An hidden file .Renviron has been created (or modified) in you personal folder and contains the line PATH='${RTOOLS40_HOME}\\usr\\bin;${PATH}' (the path to Rtools4). At each start will read this file.

Restart RStudio and check that find make:

Sys.which("make")
## "C:/rtools40/usr/bin/make.exe"

Now you can install packages from sources:

install.packages("jsonlite", type = "source")


  1. Install pandoc

The package rmarkdown requires the software pandoc to convert document from one format (e.g. .Rmd) to another (e.g. .html).

Download pandoc at: https://github.com/jgm/pandoc/releases/tag/2.16.1, and follow instructions.


  1. Install a LaTeX distribution

If you want to convert documents from .Rmd to .pdf, you will need LaTeX. You can install a lightened distribution using the package tinytex. Open RStudio and run:

## Install tinytex ----
install.packages("tinytex")

## Install LaTeX ----
tinytex::install_tinytex()

N.B. If you already have a LaTeX distribution, do not install tinytex.


 macOS

Th easiest way to set up your environment is by using the package manager for macOS Homebrew. With this tool you can install (and update) your software directly from the terminal. First you need to install the Apple software Command Line Tools for Xcode.

Open a terminal, run this line and accept the license:

## Install Xcode Command Line Tools ----
sudo xcode-select --install

Then you can install Homebrew:

## Install Homebrew ----
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

## Check installation ----
brew --version
## Homebrew 3.3.8

## Update repositories (list of available software) ----
brew update

Follow these lines to install your environment:

## Install utilities for R ----
brew install --cask xquartz     ## Graphical window system
brew install gfortran           ## FORTRAN compiler for GCC

## Install R ----
brew install --cask r

## Install RStudio Desktop ----
brew install --cask rstudio

## Install git ----
brew install git

## Install pandoc ----
brew install pandoc             ## Convert documents (Rmd, html, etc.)

Open RStudio and check your installation:

R.version.string
## "R version 4.1.2 (2021-11-01)"

Sys.which("git")
##            git
## "/usr/bin/git"

Sys.which("make")
##            make 
## "/usr/bin/make"

Sys.which("pandoc")
##            pandoc 
## "/usr/local/bin/pandoc"


If you want to convert documents from .Rmd to .pdf, you will need LaTeX. You can install a lightened distribution using the package tinytex. Open RStudio and run:

## Install tinytex ----
install.packages("tinytex")

## Install LaTeX ----
tinytex::install_tinytex()


  Congrats! You have successfully install your environment. The following commands will install system libraries (software) required by some packages (sf, sp, rgdal, rgeos, raster, terra, xml, etc.).

## Install other utilities for R (spatial and others) ----
brew install libgit2 
brew install libxml2 
brew install poppler 
brew install geos gdal netcdf proj udunits

To keep your system up-to-date run frequently:

## Update environment ----
brew update && brew upgrade --greedy


 Ubuntu

To keep your system up-to-date, do not forget to frequently run:

## System update ----
sudo apt update && sudo apt upgrade


Then check your version of Ubuntu:

## Check Ubuntu version ----
cat /etc/issue
## Ubuntu 20.04 LTS


Table 1. List of CRAN repositories for different versions of Ubuntu
Release Release name Repository URL
21.04 Hirsute Hippo https://cloud.r-project.org/bin/linux/ubuntu hirsute-cran40/
20.10 Groovy Gorilla https://cloud.r-project.org/bin/linux/ubuntu groovy-cran40/
20.04 LTS Focal Fossa https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/
18.04 LTS Bionic Beaver https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/
16.04 LTS Xenial Xerus https://cloud.r-project.org/bin/linux/ubuntu xenial-cran40/


Install by following these steps (change the URL below by the one specific to your version of Ubuntu, cf. Table 1) :

## Install APT utilities ----
sudo apt install software-properties-common

## Add CRAN GPG key ----
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

## Add CRAN repository to APT repositories list ----
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'

## Update packages list ----
sudo apt update

## Install R ----
sudo apt install r-base

## Install R (for development, recommended) ----
sudo apt install build-essential
sudo apt install r-base-dev


To install RStudio Desktop visit this page: https://www.rstudio.com/products/rstudio/download/#download, download the .deb file for Ubuntu 18/Debian 10 et install it. If you have an older version of Ubuntu (16.04), visit this page: https://www.rstudio.com/products/rstudio/older-versions/

Alternatively, you can install RStudio Desktop from the terminal:

## Install wget utility ----
sudo apt install wget

## Download RStudio Desktop installer (Ubuntu >= 18.04) ----
wget https://download1.rstudio.org/desktop/bionic/amd64/rstudio-2021.09.0-351-amd64.deb

## Download RStudio Desktop installer (Ubuntu 16.04 only) ----
# wget https://download1.rstudio.org/desktop/xenial/amd64/rstudio-1.4.1106-amd64.deb

## Install RStudio Desktop ----
sudo dpkg -i rstudio-2021.09.0-351-amd64.deb
# sudo dpkg -i rstudio-1.4.1106-amd64.deb

## Remove installer ----
rm rstudio-2021.09.0-351-amd64.deb
# rm rstudio-1.4.1106-amd64.deb


Now let’s install git:

sudo apt install git

Finally, install pandoc (to convert documents using rmarkdown):

sudo apt install pandoc


Open RStudio and check your installation:

R.version.string
## "R version 4.1.2 (2021-11-01)"

Sys.which("git")
##            git
## "/usr/bin/git"

Sys.which("make")
##            make 
## "/usr/bin/make"

Sys.which("pandoc")
##            pandoc 
## "/usr/bin/pandoc"


If you want to convert documents from .Rmd to .pdf, you will need LaTeX. You can install a lightened distribution using the package tinytex. Open RStudio and run:

## Install tinytex ----
install.packages("tinytex")

## Install LaTeX ----
tinytex::install_tinytex()


  Congrats! You have successfully install your environment. The following commands will install system libraries (software) required by some packages (sf, sp, rgdal, rgeos, raster, terra, xml, etc.).

## Install other utilities for R (spatial and others) ----
sudo apt install libgdal-dev libproj-dev libgeos-dev libudunits2-dev libnetcdf-dev
sudo apt install libcairo2-dev libmagick++-dev
sudo apt install libpoppler-cpp-dev

To keep your system up-to-date, do not forget to frequently run:

## Update system ----
sudo apt update && sudo apt upgrade


Configuration

git

To use git, you need to store your credentials (user name and email) that will be added to all your commits. Open a terminal (you can use the terminal of RStudio) and run:

git config --global user.name  "Jane Doe"
git config --global user.email "jane.doe@mail.com"

When you create a new git project the default branch will be named master. It’s recommended to replace master by main (for more information read this article).

On a terminal, run:

git config --global init.defaultBranch "main"

From now when you will initialize git in a new project the default branch will be main.


 GitHub SSH keys

To communicate with GitHub, you can use the protocol HTTPS (default) or the protocol SSH (recommended). The protocol SSH is a more secure method of logging into a remote server (GitHub). To use this protocol, you need to generate an SSH keys pair that consists in creating two long strings of characters: a public key and a private key. You can place the public key on any server (for instance GitHub), and then connect to the server using a device (your laptop) that has access to the private key.

To generate a new SSH keys pair proceed as follow:

     Create an account on GitHub at https://github.com/signup
     Open RStudio and click on: Tools > Global options > Git/SVN
     Click on Create RSA Key and then click on Create (you can omit the passphrase). This new SSH keys pair has been stored in ~/.ssh/.
     Click on View public key and copy it
     Go to the page https://github.com/settings/keys and click on New SSH key
     Choose a name for your key (e.g. your laptop name) and paste your public key.
     Finally click on Add SSH key


  If you are on Ubuntu you need to install the following utility:

sudo apt install ssh-askpass


Let’s test the SSH connection with GitHub. Open a terminal and run (you need to accept the fingerprint, just enter yes):

ssh -T git@github.com

Congrats! You can now communicate with GitHub using the SSH protocol.


  From now, you need to use the SSH URL when connecting a new GitHub project (named remote).

## URL HTTPS ----
https://github.com/frbcesab/datatoolbox.git

## URL SSH (use this one) ----
git@github.com:frbcesab/datatoolbox.git


  If you lose your private SSH key you won’t be able to establish a connection with GitHub. You’ll need to delete your SSH key on GitHub and to create a new one.


 GitHub PAT

If you want to use the GitHub API, essentially with the package usethis, you need to create a GitHub Personal Access Token (PAT).

Go to https://github.com/settings/tokens

     Click on Generate new token
     Choose a name for your new token (e.g. your laptop name)
     In the section scopes, select at least repo and workflow
     Copy this token.

Then open RStudio and run:

## Install usethis package ----
install.packages("usethis")

## Open ~/.Renviron file ----
usethis::edit_r_environ()
## • Modify '~/.Renviron'
## • Restart R for changes to take effect

In the ~/.Renviron file, add this new line: GITHUB_PAT='XXX' where XXX is your token. Do not forget to restart .


Finally let’s tell usethis to use the SSH protocol. Open RStudio and run:

## Install usethis package ----
install.packages("usethis")

## Open ~/.Rprofile file ----
usethis::edit_r_profile()
## • Modify '~/.Rprofile'
## • Restart R for changes to take effect

In the ~/.Rprofile file, add this new line: options(usethis.protocol = "ssh"). Do not forget to restart .

Corrections

If you see mistakes or want to suggest changes, please Create an issue on the source repository.

Reuse

The material of this website is licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/rdatatoolbox/rdatatoolbox.github.io/.

Citation

Casajus N, Dray S, Gimenez O, Guéry L, Guilhaumon F & Schiettekatte NMD (2021) Workshop FRB-CESAB & GdR EcoStat: Data Toolbox for Reproducible Research in Ecology (Edition 2021). Zenodo. http://doi.org/10.5281/zenodo.4262978.